package com.zmm.member.service.impl;

import com.zmm.common.enums.RegistryMethodEnums;
import com.zmm.common.enums.UserFlagType;
import com.zmm.common.enums.UserRegisterSourceEnum;
import com.zmm.common.enums.UserTempCustomerEnum;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.member.dao.CustomerDao;
import com.zmm.member.model.Customer;
import com.zmm.member.service.RegistryService;
import com.zmm.member.submit.vo.RegisterLoginVo;
import com.zmm.util.code.GenerateNumberUtil;
import com.zmm.util.convert.StringUtil;
import com.zmm.util.password.SecurityUtil;
import com.zmm.util.time.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 900045
 * @description:
 * @name RegistryServiceImpl
 * @date By 2021-07-26 16:09:53
 */
@Service
public class RegistryServiceImpl implements RegistryService {

	@Resource
	private ThreadPoolExecutor threadPoolExecutor;
	
	@Resource
	private CustomerDao customerDao;

	/**
	 * 手机号 注册用户 -- 实现
	 * @author: 900045
	 * @date: 2021-07-26 16:14:45
	 * @throws BusinessException
	 * @param registerLoginVo: 注册提交的 vo
	 * @param method: 注册方式   
	 * @return: com.zmm.member.model.Customer
	 **/
	@Override
	public Customer registryByPhone(RegisterLoginVo registerLoginVo, RegistryMethodEnums method) throws BusinessException {
		return registryByPhone(registerLoginVo.getPhone(), registerLoginVo.getPassword(), registerLoginVo.getVerifyCode(), 
				registerLoginVo.getType(), registerLoginVo.getInvitationCode(), method, registerLoginVo.getIp(),
				null, null);
	}

	/**
	 * 三方(微信) 注册用户 -- 实现
	 * @author: 900045
	 * @date: 2021-07-26 16:15:04
	 * @throws 
	 * @param phone: 手机号
	 * @param password: 密码
	 * @param verifyCode: 短信验证码
	 * @param type: 验证码类型
	 * @param invitationCode: 邀请码
	 * @param method: 注册方式
	 * @param ip:   
	 * @param nickName: 用户昵称
	 * @param iconUrl: 用户头像
	 * @return: com.zmm.member.model.Customer
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public Customer registryByPhone(String phone, String password, String verifyCode, Integer type, String invitationCode,
									RegistryMethodEnums method, String ip, String nickName, String iconUrl) throws BusinessException {
		// step1. 包装 Customer 对象 --> 新增
		Customer customer = packagingCustomer(phone, invitationCode, ip, nickName, iconUrl);

		Integer customerId = customer.getCustomerId();
		// step2. 生成盐值
		String saltVal = SecurityUtil.getNewPsw();
		switch (method) {
			case PASSWORD:
				
				// step3. 生成密码
				String encryptPwd = generatePassword(saltVal, password, customerId);
				customer.setCustomerPassword(encryptPwd);
				customer.setUserSaltVal(saltVal);
				break;
			case VERIFY_CODE:
				// 如果是验证码注册 / 默认密码是 手机后6位(特殊情况下 默认密码 是 123456)
				String encryptCode = generatePassword(saltVal, StringUtil.phoneAfterSix(phone), customerId);
				customer.setCustomerPassword(encryptCode);
				customer.setUserSaltVal(saltVal);
				break;
				default:
					break;
		}
		
		// step4. 生成用户邀请码
		String myInvitationCode = GenerateNumberUtil.customerNumberId(customerId);
		
		// step5. 更新用户信息
		
		customer.setUserUniqueCode(String.valueOf(customerId));
		customer.setInvitationCode(myInvitationCode);
		customerDao.updateCustomerByCustomerId(customer);
		// step6.
		// 成功 --> 发送 mq 消息
		return customer;
	}
	
	/**
	 * step1. 包装 Customer 对象 --> 新增
	 * @author: 900045
	 * @date: 2021-07-26 16:23:44
	 * @throws 
	 * @param phone: 
	 * @param invitationCode:
	 * @param ip:   
	 * @param iconUrl:
	 * @param nickName:   
	 * @return: com.zmm.member.model.Customer
	 **/
	private Customer packagingCustomer(String phone, String invitationCode, String ip, String nickName, String iconUrl){
		Customer customer = new Customer();
		// 设置手机已绑定
		customer.setWhetherMobile(1);
		customer.setCustomerUsername(phone);
		customer.setWhetherFlag(UserFlagType.FLAG_NO.getCode());
		customer.setLoginIp(ip);
		if (StringUtils.isNotEmpty(invitationCode)) {
			customer.setParentInvitationCode(invitationCode);
		}
		if (StringUtils.isEmpty(nickName)) {
			// 昵称可重复，根据当前毫秒数生成昵称
			customer.setCustomerNickname("zmm_" + System.currentTimeMillis());
		} else {
			customer.setCustomerNickname(nickName);
		}
		if (StringUtils.isNotEmpty(iconUrl)) {
			customer.setCustomerImg(iconUrl);
		}
		customer.setWhetherTempCustomer(UserTempCustomerEnum.YES.getCode());
		customer.setCreateTime(DateUtil.getNowTime());
		customer.setRegisterSource(UserRegisterSourceEnum.APP.getCode());
		customerDao.saveCustomer(customer);
		return customer;
	}
	
	/**
	 * step3. 生成密码
	 * @author: 900045
	 * @date: 2021-07-26 17:07:39
	 * @throws 
	 * @param saltVal: 
	 * @param password: 
	 * @param customerId: 
	 * @return: java.lang.String
	 **/
	private String generatePassword(String saltVal, String password, Integer customerId){
		
		//生成加密后的登陆密码
		String encryptPwd = password;

		if (null != password) {
			encryptPwd = SecurityUtil.getStoreLoginPwd(String.valueOf(customerId), password, saltVal);
		}
		return encryptPwd;
	}
}
